package control;

import gui.DialogCollection;

import java.awt.CardLayout;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import data.Player;

public class SelectPlayersMenuManager implements ActionListener, Manager{
	
	private Control control;
	public SelectPlayersMenuManager(Control c){
		control = c;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		CardLayout cl = (CardLayout)(control.gui.getMenus().getLayout());
		if (e.getActionCommand().equals("BACK")){
			IOManager.saveOptions(control.options);
	        cl.show(control.gui.getMenus(), "MainMenu");
			
		}else if (e.getActionCommand().equals("START GAME")){
			int i = 0;
			for (Player pl : control.players){
				if( pl.isEnabled()){
					i++;
				}
			}
			if ( i < 2){
				DialogCollection.showNotEnoughPlayerDialog();
			}else{
				IOManager.saveOptions(control.options);
				control.startGames();
			}
		}
	}

	public boolean playerRequestsStateChange(Player pl, boolean enabled) {
		pl.setEnabled(enabled);
		return true;
	}

	public boolean playerRequestsNameChange(Player pl, String newName) {
		if(newName.isEmpty() || newName.length() > 10)return false;
		else if(newName.contains("\\/\n\t\b\f\r\"\'"))return false;
		else {
			pl.setName(newName);
			return true;
		}
		
	}

	public boolean playerRequestsBuzzKeyChange(Player pl, int keyCode, int modifiers) {
//		if(modifiers != 0) return false;
		if(control.kim.isKeyInUse(keyCode) || control.kim.isBadKey(keyCode))return false;
		else {
			pl.setBuzzerKeyCode(keyCode);
			return true;
		}
		
		
	}
}